package Service;

import Excption.moneyIsNotEnven;
import Mapper.InputBillMapper;
import Mapper.OutputBillMapper;
import Utils.RandomId;
import pojo.Bill;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class BillService {
    private InputBillMapper inputBillMapper = new InputBillMapper();
    private OutputBillMapper outputBillMapper = new OutputBillMapper();

    public boolean inputBill(String date, double money, String billName, String billType, String comment) throws ParseException, SQLException {
        Bill bill = new Bill();
        String datePattern = "YYYY-MM-DD";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
        Date localDate = simpleDateFormat.parse(date);
        RandomId randomId = new RandomId();
        int billid = randomId.getRandomId();
        bill.setDate(localDate);
        bill.setBillId(billid);
        bill.setMoney(money);
        bill.setComment(comment);
        bill.setBillType(billType);
        bill.setBillName(billName);
        int storage = inputBillMapper.storage(bill);
        if (storage > 0) {
            return true;
        }
        return false;
    }

    public boolean outputBill(String date, double money, String billName, String billtype, String commment) throws ParseException, SQLException, moneyIsNotEnven {
        Bill bill = new Bill();
        if (money < 0) {
            throw new moneyIsNotEnven();
        }
        String datePattern = "YYYY-MM-DD";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
        Date dateddate = simpleDateFormat.parse(date);
        RandomId randomId = new RandomId();
        int billid = randomId.getRandomId();
        bill.setDate(dateddate);
        bill.setBillId(billid);
        bill.setMoney(money);
        bill.setComment(commment);
        bill.setBillType(billtype);
        bill.setBillName(billName);
        int i = outputBillMapper.outputBill(bill);
        if (i > 0) {
            return true;
        }
        return false;

    }

    public List<Bill> getOutputBills() throws Exception {
        List<Bill> bills = outputBillMapper.getOutputBills();
        if (bills == null) {
            return null;
        }
        return bills;
    }

    public List<Bill> getInputBill() {
        List<Bill> bills = inputBillMapper.getInputBills();
        if (bills == null) {
            return null;
        }
        return bills;
    }

    public List<Bill> getInputBillForTime(String date) {
        Locale locale = Locale.CHINA;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-DD",locale);
        List<Bill> billList;
        try {
            Date formatDate = simpleDateFormat.parse(date);
            billList = inputBillMapper.getInputAndOutputBill(date);
            if (billList == null) {
                return null;
            }
        } catch (ParseException e) {
            throw new RuntimeException("传递的时间格式错误");
        }
        return billList;
    }
    public List<Bill> getOutputBills(String date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-DD");
        List<Bill> billList;
        try {
            Date formatDate = simpleDateFormat.parse(date);
            billList = outputBillMapper.getInputBillForTime(formatDate);
            if (billList == null) {
                return null;
            }
        } catch (ParseException e) {
            throw new RuntimeException("传递的时间格式错误");
        }
        return billList;
    }
     /*查询月度支出的账单*/
    public List <List<Bill>> getMonthBill(){
        List<Bill> monthBill = inputBillMapper.getMonthBill();
        List<Bill> monthBill1 = outputBillMapper.getMonthBill();
         List <List<Bill>> lists =  new ArrayList<>() ;
         lists.add(monthBill);
         lists.add(monthBill1);
         return  lists;
    }
    public List <List<Bill>> getMonthBill(int year ,  int month){
        List<Bill> monthBill = inputBillMapper.getMonthBill(year , month);
        List<Bill> monthBill1 = outputBillMapper.getMonthBill(year , month);
        List <List<Bill>> lists =  new ArrayList<>() ;
        if (monthBill1 == null || monthBill1.size()==0){
            lists.add(new ArrayList<Bill>());
            lists.add(monthBill1);
            return  lists;
        }
        lists.add(monthBill);
        lists.add(monthBill1);
        return  lists;
    }


}
